<?php

require_once (dirname ( _FILE_ ) . "/../../config/config.php");

$country = $_REQUEST ['country'];
$category = $_REQUEST ['category'];
$title = $_REQUEST ['title'];
$year = $_REQUEST ['year'];
$program_id = $_REQUEST ['program_id'];

if (isset ( $_REQUEST ['country'] )) {
	$sql = "SELECT COUNT(l.program_id) AS num,t.program_id, t.title, t.pic_link_small
		  FROM zy_iptvprogram_list t LEFT OUTER JOIN zy_iptv_link l ON 
	 	  t.`program_id`=l.`program_id` WHERE t.`country`=$country GROUP BY t.program_id";
	
	$rs = mysql_query ( $sql );
	
	$message = array ();
	
	while ( $row = mysql_fetch_array ( $rs ) ) {
		$message [] = array (
				'num' => $row ['num'],
				'program_id' => $row ['program_id'],
				'title' => $row ['title'],
				'pic_link_small' => $row ['pic_link_small'] 
		);
	}
	echo json_encode($message);
	
} elseif (isset ( $_REQUEST ['category'] )) {
	$sql = "SELECT COUNT(l.program_id) AS num,t.program_id, t.title, t.pic_link_small
	      FROM zy_iptvprogram_list t LEFT OUTER JOIN zy_iptv_link l ON 
 	      t.`program_id`=l.`program_id` WHERE t.`category`=$category GROUP BY t.program_id";
	
	$rs = mysql_query ( $sql );
	
	$message = array ();
	
	while ( $row = mysql_fetch_array ( $rs ) ) {
		$message [] = array (
				'num' => $row ['num'],
				'program_id' => $row ['program_id'],
				'title' => $row ['title'],
				'pic_link_small' => $row ['pic_link_small'] 
		);
	}
	echo json_encode($message);
	
} elseif (isset ( $_REQUEST ['title'] )) {
	$sql = "SELECT COUNT(l.program_id) AS num,t.program_id, t.title, t.pic_link_small
          FROM zy_iptvprogram_list t LEFT OUTER JOIN zy_iptv_link l ON 
   	      t.`program_id`=l.`program_id` WHERE t.`title`='$title' GROUP BY t.program_id";
	
	$rs = mysql_query ( $sql );
	
	$message = array ();
	
	while ( $row = mysql_fetch_array ( $rs ) ) {
		$message [] = array (
				'num' => $row ['num'],
				'program_id' => $row ['program_id'],
				'title' => $row ['title'],
				'pic_link_small' => $row ['pic_link_small'] 
		);
	}
	echo json_encode($message);
	
} elseif (isset ( $_REQUEST ['year'] )) {
	if (isset ( $_REQUEST ['program_id'] )) {
		$a = $program_id + 1;
		$b = $a + 32;
		$sql = "SELECT COUNT(l.program_id) AS num,t.program_id, t.title, t.pic_link_small
              FROM zy_iptvprogram_list t LEFT OUTER JOIN zy_iptv_link l ON 
   	          t.`program_id`=l.`program_id` WHERE year='$year' GROUP BY t.program_id limit $a,$b ";
		
		$rs = mysql_query ( $sql );
		
		$message = array ();
		
		while ( $row = mysql_fetch_array ( $rs ) ) {
			$message [] = array (
					'num' => $row ['num'],
					'program_id' => $row ['program_id'],
					'title' => $row ['title'],
					'pic_link_small' => $row ['pic_link_small'] 
			);
		}
		echo json_encode($message);
	} else {
		$sql = "SELECT COUNT(l.program_id) AS num,t.program_id, t.title, t.pic_link_small
              FROM zy_iptvprogram_list t LEFT OUTER JOIN zy_iptv_link l ON 
   	          t.`program_id`=l.`program_id` WHERE year='$year' GROUP BY t.program_id limit 32 ";
		
		$rs = mysql_query ( $sql );
		
		$message = array ();
		
		while ( $row = mysql_fetch_array ( $rs ) ) {
			$message [] = array (
					'num' => $row ['num'],
					'program_id' => $row ['program_id'],
					'title' => $row ['title'],
					'pic_link_small' => $row ['pic_link_small'] 
			);
		}
		echo json_encode($message);
	}
}

